import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;

class Fun  implements ActionListener{
    Connection connection;
    JFrame frame=new JFrame();
    JLabel lb1;//顶部需要的组件
    Button bt1,bt2,bt3,bt4;
    //定义中间所需要的组件
    // 中部有4个panel，选项卡窗口管理
    JTabbedPane pane;
    JPanel panel_s,panel_t;
    JLabel label1,label2;
    JLabel label3,label4;
    JLabel stu;//用来提示这是学生的登录界面,学生默认输入长度为11位
    JLabel tea;//用来提示这是老师的登录界面,老师默认输入长度为3位

    JTextField textField_student;//学生区域的学生输入框
    JPasswordField Password_student;//密码输入框
    JTextField textField_teacher;//老师区域输入框
    JPasswordField Password_teacher;//老师区域密码输入框

    //底部所需要的组件
    JPanel jPanel1;
    JButton jButton;

    //3学生注册界面相关控件
    JPanel stu_register;
    JLabel S_register_lb1,S_register_lb2;
    JTextField S_register;
    JPasswordField S_pass;
    JLabel Student;

    //4老师注册界面相关控件
    JPanel tea_register;
    JLabel T_register_lb1,T_register_lb2;
    JTextField T_register;
    JPasswordField T_pass;
    JLabel Teacher;
    @Override
    public void actionPerformed(ActionEvent e) {
    }
    public Fun()
    {

        //数据库连接
        jdbcConnection jdbc=new jdbcConnection();
        connection=jdbc.giveConnection();

        //处理底部
        lb1=new JLabel(new ImageIcon("image/upper.jpg"));
        jPanel1=new JPanel();
//        jButton=new JButton("确定");
//        jPanel1.add(jButton);//将button组件加进去
        //后面舍弃了这种做法直接四个界面加入button应对不同的情况

        //处理中部
        panel_s=new JPanel(new GridLayout(3,3));
        label1=new JLabel("学生账号",JLabel.CENTER);
        label2=new JLabel("学生密码",JLabel.CENTER);
        textField_student=new JTextField();
        Password_student=new JPasswordField();
        stu=new JLabel("这里是学生登录界面",JLabel.CENTER);
        bt1=new Button("login");
        //按控件顺序加入到每个板面
        panel_s.add(label1);
        panel_s.add(textField_student);
        panel_s.add(label2);
        panel_s.add(Password_student);
        panel_s.add(stu);
        panel_s.add(bt1);

        //老师
        panel_t=new JPanel(new GridLayout(3,3));
        label3=new JLabel("老师账号",JLabel.CENTER);
        label4=new JLabel("老师密码",JLabel.CENTER);
        textField_teacher=new JTextField();
        Password_teacher=new JPasswordField();
        bt2=new Button("login");
        tea=new JLabel("这里是老师登录界面",JLabel.CENTER);

        panel_t.add(label3);
        panel_t.add(textField_teacher);
        panel_t.add(label4);
        panel_t.add(Password_teacher);
        panel_t.add(tea);
        panel_t.add(bt2);

        //学生注册
        stu_register=new JPanel(new GridLayout(3,3));
        S_register_lb1=new JLabel("注册学生账号",JLabel.CENTER);
        S_register_lb2=new JLabel("注册学生密码",JLabel.CENTER);
        S_register=new JTextField();
        S_pass=new JPasswordField();
        Student=new JLabel("这里是学生注册",JLabel.CENTER);
        bt3=new Button("register");

        Student.setForeground(Color.RED);

        stu_register.add(S_register_lb1);
        stu_register.add(S_register);
        stu_register.add(S_register_lb2);
        stu_register.add(S_pass);
        stu_register.add(Student);
        stu_register.add(bt3);
        //老师注册
        tea_register=new JPanel(new GridLayout(3,3));
        T_register_lb1=new JLabel("注册老师账号",JLabel.CENTER);
        T_register_lb2=new JLabel("注册学生密码",JLabel.CENTER);
        T_register=new JTextField();
        T_pass=new JPasswordField();
        Teacher=new JLabel("这里是进行老师注册",JLabel.CENTER);
        bt4=new Button("register");

        Teacher.setForeground(Color.BLUE);
        tea_register.add(T_register_lb1);
        tea_register.add(T_register);
        tea_register.add(T_register_lb2);
        tea_register.add(T_pass);
        tea_register.add(Teacher);
        tea_register.add(bt4);

        //创建选项卡窗口
        //加入页面

        pane=new JTabbedPane();
        pane.add("学生登录",panel_s);
        pane.add("老师登录",panel_t);
        pane.add("学生注册",stu_register);
        pane.add("老师注册",tea_register);
        frame.setSize(550,340);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.add(lb1,"North");
        frame.add(jPanel1,"South");
        frame.add(pane,"Center");

        bt1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserView view=  new UserView(1);
                frame.dispose();
                // view.number=1;
            }
        });
        bt2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserView view=new UserView(2);

            }
        });
        bt3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    //读取账号和密码
                    String account= S_register.getText();
                    String password=new String(S_pass.getPassword());
                    //进行数字的判断
                    int number=Integer.parseInt(account);
                    //进行数字长度的判断
                    if(account.length()==6){
                        new InsertStudentInformation(frame,connection,account,password);
                    }
                    else{
                        JOptionPane.showMessageDialog(
                                frame,
                                "账号不合法，注意为6位数字",
                                "Error",
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                }catch (Exception exception){
                    JOptionPane.showMessageDialog(
                            frame,
                            "账号不合法，注意为三位数字",
                            "Error",
                            JOptionPane.INFORMATION_MESSAGE
                    );
                }
                //将学生的数据存入数据库
            }
        });
        bt4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                try{
                    //读取账号和密码
                    String account= T_register.getText();
                    String password=new String(T_pass.getPassword());
                    //进行数字的判断
                    int number=Integer.parseInt(account);
                    //进行数字长度的判断
                    if(account.length()==3){
                        new InsertTeacherInformation(frame,connection,account,password);
                    }
                    else{
                        JOptionPane.showMessageDialog(
                                frame,
                                "账号不合法，注意为3位数字",
                                "Error",
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                }catch (Exception exception){
                    JOptionPane.showMessageDialog(
                            frame,
                            "账号不合法，注意为三位数字",
                            "Error",
                            JOptionPane.INFORMATION_MESSAGE
                    );
                }
                //老师的数据存入数据库
            }
        });
    }


}

public class Main {
    public static void main(String[] args) {
        Fun Fun=new Fun();
    }
}